package br.com.duduso.conferencia_claro_desktop.utils;

import br.com.duduso.conferencia_claro_desktop.mail.MailJava;
import br.com.duduso.conferencia_claro_desktop.mail.MailJavaSender;
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

/**
 * Classe Para Recuperar E-mails
 * Gmail e Yahoo
 * @author duduso
 */
public class EmailUtils {

    public static MultiPartEmail getEmailYahoo(String emailDestino, String nomeDestino,
            String tituloEmail, String mensagemEmail) throws EmailException{

            if(StringUtils.isBlank(emailDestino)
                    || StringUtils.isBlank(nomeDestino)
                    || StringUtils.isBlank(tituloEmail)
                    || StringUtils.isBlank(mensagemEmail)){
                    return null;
            }
            
            MultiPartEmail email = new MultiPartEmail();
            email.setHostName("smtp.mail.yahoo.com.br"); // o servidor SMTP para envio do e-mail
            email.setSmtpPort(587);
            email.setAuthentication("elaine_oliveira18", "221206");
            email.addTo(emailDestino, nomeDestino); //destinatário
            email.setFrom("elaine_oliveira18@yahoo.com.br", "Elaine Oliveira"); // remetente
            email.setSubject(tituloEmail); // assunto do e-mail
            email.setMsg(mensagemEmail); //conteudo do e-mail

            return email;

    }

    public static MultiPartEmail getEmailGmail(String emailDestino, String nomeDestino,
            String tituloEmail, String mensagemEmail) throws EmailException {

            if(StringUtils.isBlank(emailDestino)
                    || StringUtils.isBlank(nomeDestino)
                    || StringUtils.isBlank(tituloEmail)
                    || StringUtils.isBlank(mensagemEmail)){
                    return null;
            }

            MultiPartEmail email = new MultiPartEmail();
            email.setHostName("smtp.gmail.com");
            email.setSmtpPort(587);
            email.setAuthentication("elaine.oliveira@grupolagoa.com.br", "dudu@221206");
            email.addTo(emailDestino, nomeDestino);
            email.setFrom("elaine.oliveira@grupolagoa.com.br", "Elaine Oliveira");
            email.setSubject(tituloEmail);
            email.setMsg(mensagemEmail);
            email.setSSL(true);

            return email;
        
    }

    public static EmailAttachment getAnexo(String caminhoArquivo,
            String descricao, String nome){

            if(StringUtils.isBlank(caminhoArquivo)
                    || StringUtils.isBlank(descricao)
                    || StringUtils.isBlank(nome)){
                    return null;
            }

            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath(caminhoArquivo);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription(descricao);
            attachment.setName(nome);
            
            return attachment;

    }

    public static void enviarEmailGmail(String tituloEmail, String corpoEmail, 
            String emailDestino, String nomeDestino, String... pathFiles){

            MailJava gmail = new MailJava();
            gmail.setSmtpHostMail("smtp.gmail.com");
            gmail.setSmtpPortMail("587");
            gmail.setSmtpAuth("true");
            gmail.setSmtpStarttls("true");
            gmail.setUserMail("elaine.oliveira@grupolagoa.com.br");
            gmail.setPassMail("dudu@221206");
            gmail.setFromNameMail("Elaine Oliveira");
            gmail.setCharsetMail("ISO8859-1");
            gmail.setSubjectMail(tituloEmail);
            gmail.setBodyMail(corpoEmail);
            gmail.setTypeTextMail(MailJava.TYPE_TEXT_HTML);
            gmail.getToMailsUsers().put(emailDestino, nomeDestino);
            gmail.getFileMails().addAll(Arrays.asList(pathFiles));

            try {
                    new MailJavaSender().senderMail(gmail);
            } catch (Exception e) {
                    e.printStackTrace();
            }

    }

}
